package com.sls.autoz.auth;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Objects;

import android.util.Base64;

import org.json.JSONObject;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class MainActivity extends AppCompatActivity {

    String salt1 = "auto-cj-dz-sls";

    String salt2 = "auto-xj-sls";

    EditText macView;

    TextView getCodeView;

    EditText showCodeView;

    CalendarView calendarView;

    EditText timeView;

    String time = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView(){
        macView = findViewById(R.id.mac);
        getCodeView = findViewById(R.id.get_code);
        showCodeView = findViewById(R.id.code);
        showCodeView.setSelectAllOnFocus(true);
        calendarView = findViewById(R.id.calendarView);
        timeView = findViewById(R.id.time);

        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
                //显示用户选择的日期
                Toast.makeText(MainActivity.this,year + "-" + (month+1)  + "-" + dayOfMonth ,Toast.LENGTH_SHORT).show();
                timeView.setText(year + "-" + (month+1) + "-" + dayOfMonth );
                time = year + "-" + (month+1) + "-" + dayOfMonth;
            }
        });
        getCodeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String mac = macView.getText().toString();
                if(mac.equals("")){
                    Toast.makeText(getApplicationContext(),"请输入机器码",Toast.LENGTH_LONG).show();
                    return;
                }
                if(time.equals("")){
                    Toast.makeText(getApplicationContext(),"请输入过期时间",Toast.LENGTH_LONG).show();
                    return;
                }
                Calendar c = Calendar.getInstance();
                try {
                    c.setTime((new SimpleDateFormat("yyyy-MM-dd").parse(time)));
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("mac",mac);
                    jsonObject.put("time",c.getTimeInMillis());
                    //注册码
                    showCodeView.setText(AESEncrypt.encrypt(jsonObject.toString(),salt1));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }


}